﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain;
using Yunwei.IBizLogic;
using Yunwei.Web.Models;
using Microsoft.AspNet.Identity;

namespace Yunwei.Web.Controllers
{
    public class ShoppingCartController : Controller
    {
        IFoodService foodService;
        IShoppingCartService cartService;
        IUserService userService;
        public ShoppingCartController()
        {
            foodService = new FoodService();
            cartService = new ShoppingCartService();
            userService = new UserService();
        }

        // GET: ShoppingCart
        [Authorize]
        public ActionResult Index()
        {
            int user_Id = User.Identity.GetUserId<int>();
            ViewBag.User = userService.Find(user_Id);

            var carts = cartService.FindList(50, user_Id, 0, 0, 0);
            
            ShoppingCartsViewModel scsvm = new ShoppingCartsViewModel();

            foreach(var cart in carts)
            {
                scsvm.ShoppingCarts.Add(ShoppingCartViewModel.FromShoppingCart(cart));
            }
            if (scsvm.ShoppingCarts.Count == 0)
                return Redirect("~/");

            return View(scsvm);
        }

        [HttpPost]
        public ActionResult Add(ShoppingCartViewModel svm)
        {
            RespMessage<ShoppingCartViewModel> rcm = new RespMessage<ShoppingCartViewModel>();

            if (Request.IsAuthenticated)
            {
                var food = foodService.Find(svm.Food_Id);
                if (food == null || food.Id <= 0)
                {
                    rcm.Success = false;
                }
                else
                {
                    int user_Id = User.Identity.GetUserId<int>();
                    var sc = cartService.FindByUserId(user_Id, svm.Food_Id);
                    if (sc == null || sc.Id <= 0)
                    {
                        svm.Restaurant_Id = food.Restaurant_Id;
                        svm.Chef_Id = food.Chef_Id;
                        svm.User_Id = user_Id;
                        rcm.Success = cartService.Add(svm.ShoppingCart) != null;
                    }
                    else
                    {
                        sc.Total++;
                        rcm.Success = cartService.Update(sc);
                    }

                    try
                    {
                        var carts = cartService.FindList(50, user_Id, 0, 0, 0);
                        ShoppingCartsViewModel scsvm = new ShoppingCartsViewModel();
                        foreach (var cart in carts)
                        {
                            scsvm.ShoppingCarts.Add(ShoppingCartViewModel.FromShoppingCart(cart));
                        }
                        rcm.ErroMsg = scsvm.Price + "";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return Json(rcm);
            }
            else
            {
                rcm.Success = false;
                rcm.ErrorCode = 222;
                return Json(rcm);
            }
        }

        [HttpPost]
        public ActionResult Subtract(ShoppingCartViewModel svm)
        {
            RespMessage<ShoppingCartViewModel> rcm = new RespMessage<ShoppingCartViewModel>();

            if (Request.IsAuthenticated)
            {
                int user_Id = User.Identity.GetUserId<int>();
                var sc = cartService.FindByUserId(user_Id, svm.Food_Id);
                if (sc == null && sc.Id <= 0)
                {
                    rcm.Success = false;
                }
                else
                {
                    sc.Total--;
                    if (sc.Total > 0)
                        rcm.Success = cartService.Update(sc);
                    else
                        rcm.Success = cartService.Delete(sc);

                    try
                    {
                        var carts = cartService.FindList(50, user_Id, 0, 0, 0);
                        ShoppingCartsViewModel scsvm = new ShoppingCartsViewModel();
                        foreach (var cart in carts)
                        {
                            scsvm.ShoppingCarts.Add(ShoppingCartViewModel.FromShoppingCart(cart));
                        }
                        rcm.ErroMsg = scsvm.Price + "";
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            return Json(rcm);
        }
    }
}